home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / EFFECTS / FADE.C < prev    next >
C/C++ Source or Header  |  1994-04-04  |  4KB  |  185 lines

  1. // This module lets you fadding or better say
  2. // smooth palette transforming (it may be used for 
  3. // fade as it is or partial palette changing as well).
  4. // VGA 256 colors mode is assumed.
  5. //
  6. // Compiled under Borland C++ 3.1 
  7. // for large model and 286 instruction set
  8. // (c) This program is written by Kiselev J. 1994.
  9.  
  10. #include <conio.h>
  11. #include <colmodel.h>
  12.  
  13. #define BYTE unsigned char
  14. #define WORD unsigned int
  15.  
  16. typedef struct { BYTE R,G,B;} paltype;
  17. typedef struct { float Y,I,Q;} YIQtype;
  18. typedef struct { float H,L,S;} HLStype;
  19.  
  20. void setDACblock(BYTE,WORD,paltype*);
  21. void fadehls(BYTE,BYTE,BYTE,paltype*,paltype*);
  22. void fadeyiq(BYTE,BYTE,BYTE,paltype*,paltype*);
  23.  
  24. paltype palette[256];
  25. paltype palette1[256];
  26.  
  27. WORD i,j;
  28. void main(void)
  29. {
  30.     asm {
  31.         push di
  32.         mov ax,0x13
  33.         int 0x10
  34.         mov dx,200
  35.         mov ax,0xa000
  36.         mov es,ax
  37.         xor ax,ax
  38.         xor di,di
  39.     }
  40. nr:
  41.     asm {
  42.         mov cx,320
  43.         rep stosb
  44.         inc al
  45.         dec dx
  46.         jnz nr
  47.         pop di
  48.     }
  49. // create source palette (redish)
  50. for(i=0;i<256;i++)
  51. {
  52.     palette[i].R = 63-i/4;
  53.     palette[i].G = 0;
  54.     palette[i].B = 0;
  55. }
  56. // create destination palette (greenish)
  57. for(i=0;i<256;i++)
  58. {
  59.     palette1[i].R = 0;
  60.     palette1[i].G = i/4;
  61.     palette1[i].B = 0;
  62. }
  63. fadeyiq(0,255,60,palette,palette1);
  64. // fadehls(0,255,60,palette,palette1);
  65. getch();
  66. }
  67.  
  68. // the following routine provide smooth palette
  69. // transformation using YIQ color model
  70. // you should input palette range to be transform
  71. // and the number of conversion steps
  72.  
  73. void fadeyiq(BYTE first,BYTE last,BYTE step,paltype *pal,paltype *pal1)
  74. {
  75. float D,R_,G_,B_,Y,I,Q;
  76. YIQtype YIQ[256];
  77. YIQtype DYIQ[256];
  78. for (j=first;j<=last;j++)    // calculate array of increments
  79. {
  80.     // for call RGB2YIQ normalyze R,G,B value
  81.     RGB2YIQ((float)pal[j].R/63,(float)pal[j].G/63,(float)pal[j].B/63,
  82.         YIQ[j].Y,YIQ[j].I,YIQ[j].Q);
  83.     RGB2YIQ((float)pal1[j].R/63,(float)pal1[j].G/63,(float)pal1[j].B/63,
  84.         Y,I,Q);
  85.     DYIQ[j].Y = (YIQ[j].Y - Y)/step;
  86.     DYIQ[j].Q = (YIQ[j].Q - Q)/step;
  87.     DYIQ[j].I = (YIQ[j].I - I)/step;
  88. }
  89. for (i=1;i<step;i++)    // do fade (step - 1) times
  90. {
  91.     for (j=first;j<=last;j++)    // calculate intermediate palette
  92.     {
  93.         YIQ[j].I = YIQ[j].I-DYIQ[j].I;
  94.         YIQ[j].Y = YIQ[j].Y-DYIQ[j].Y;
  95.         YIQ[j].Q = YIQ[j].Q-DYIQ[j].Q;
  96.         YIQ2RGB(YIQ[j].Y,YIQ[j].I,YIQ[j].Q,R_,G_,B_);
  97.         pal[j].R = R_*63;
  98.         pal[j].G = G_*63;
  99.         pal[j].B = B_*63;
  100.     }
  101.     setDACblock(first,(last-first)+1,pal); //set VGA palette
  102. }
  103. // set VGA palette with last palette
  104. setDACblock(first,(last-first)+1,pal1);
  105. }
  106.  
  107. // the following routine provide smooth palette
  108. // transformation using HLS color model
  109.  
  110. void fadehls(BYTE first,BYTE last,BYTE step,paltype *pal,paltype *pal1)
  111. {
  112. float D,R_,G_,B_,H,L,S;
  113. HLStype HLS[256];
  114. HLStype DHLS[256];
  115. for (j=first;j<=last;j++)    // calculate array of increments
  116. {
  117.     // for call RGB2HLS normalyze R,G,B value
  118.     RGB2HLS((float)pal[j].R/63,(float)pal[j].G/63,(float)pal[j].B/63,
  119.         HLS[j].H,HLS[j].L,HLS[j].S);
  120.     RGB2HLS((float)pal1[j].R/63,(float)pal1[j].G/63,(float)pal1[j].B/63,
  121.         H,L,S);
  122.     // being aware of critical situation with Hue (negative or
  123.     // UNDEFINED)
  124.     if ((HLS[j].H != -400) && (H != -400))
  125.     {
  126.         D = HLS[j].H - H;
  127.         if (D > 0)
  128.             if (D > 180) DHLS[j].H = -(360 - D)/step;
  129.             else DHLS[j].H = D/step;
  130.         else
  131.             if (abs(D) > 180) DHLS[j].H = (360 + D)/step;
  132.             else DHLS[j].H = D/step;
  133.     }
  134.     else
  135.     {
  136.         DHLS[j].H = 0;
  137.         if (HLS[j].H == -400) HLS[j].H = H;
  138.     }
  139.     DHLS[j].L = (HLS[j].L - L)/step;
  140.     DHLS[j].S = (HLS[j].S - S)/step;
  141. }
  142. for (i=1;i<step;i++)        // do fade (step - 1) times
  143. {
  144.     for (j=first;j<=last;j++)     // calculate intermediate palette
  145.     {
  146.         HLS[j].H = HLS[j].H-DHLS[j].H;
  147.         HLS[j].L = HLS[j].L-DHLS[j].L;
  148.         HLS[j].S = HLS[j].S-DHLS[j].S;
  149.         HLS2RGB(HLS[j].H,HLS[j].L,HLS[j].S,R_,G_,B_);
  150.         pal[j].R = R_*63;
  151.         pal[j].G = G_*63;
  152.         pal[j].B = B_*63;
  153.     }
  154.     setDACblock(first,(last-first)+1,pal); // set VGA palette
  155. }
  156. // set VGA palette with last palette
  157. setDACblock(first,(last-first)+1,pal1);
  158. }
  159.  
  160. void setDACblock(BYTE first,WORD count,paltype *pal)
  161. {
  162.     asm {
  163.         push ds
  164.         push si
  165.         mov dx,0x3da
  166.     }
  167. Wait:
  168.     asm {    in al,dx
  169.         test al,8
  170.         jz Wait
  171.         cld
  172.         mov cx,count
  173.         mov ax,cx
  174.         shl cx,1
  175.         add cx,ax  // *3
  176.         mov dl,0xc8
  177.         mov al,first
  178.         out dx,al
  179.         inc dx
  180.         lds si,pal
  181.         rep outsb
  182.         pop si
  183.         pop ds
  184.     }
  185. }